package com.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.config.InterfaceModule;
import com.dto.UserInfo;
import com.service.UserService;
import com.util.MD5Util;
@RestController
public class UserAction {
	@Autowired
	private UserService service;
	@Autowired
	private MD5Util mdu;
	
	/**
	 * @时间2018年4月15日
	 * @作者:唐超
	 * 说明：登录
	 */
	@RequestMapping("Ajaxlogin.do")
	public Map<String, Object> Ajaxlogin(UserInfo user,HttpSession sess){
		Map<String, Object> json=new HashMap<String,Object>();
		String mw=mdu.getMD5tr(user.getPwd());
		user.setPwd(mw);
		user=service.login(user);
		if(user!=null) {
			sess.setAttribute("userInfo", user);
			json.put("result", true);	
		}else {
			json.put("result", false);
		}
		return json;
		
	}	


			//注册
			/**
			 * @时间2018年4月15日
			 * @作者:雷杨
			 * 说明:注册
			 */
			@RequestMapping("/addUser.do")/*@RequestParam CommonsMultipartFile img*/
			public ModelAndView  addUser(UserInfo user,HttpSession sess) {
				ModelAndView mv = new ModelAndView();
				String mw=mdu.getMD5tr(user.getPwd());
				user.setPwd(mw);
				int x = service.register(user);
				if(x > 0) {
//					return "redirect:/index.jsp";
					mv.setViewName("redirect:/index.jsp");
				}else {
//					return "redirect:/addUser.do";
					mv.setViewName("redirect:/reg.jsp");
				}
				return mv;
			}
			
			/**
			 * @时间2018年4月15日
			 * @作者:雷杨
			 * 说明://判断用户是否存在
			 */
			@RequestMapping("isUser.do")
			@ResponseBody
			public Map<String,Object>  isNoUser(String u_name) {
				Map<String,Object> map = new HashMap<>();
				UserInfo isUser = service.isNoUser(u_name);
				if(isUser != null) {
					map.put("mss", true);
				}else {
					map.put("mss", false);
				}
				return map;
			}
			
			/*
			 *注销登录
			 */
			@RequestMapping("logout.do")
			public ModelAndView  logout(HttpSession sess) {
				ModelAndView mv = new ModelAndView();
				mv.setViewName("redirect:homePage.jsp");
				sess.invalidate();//session 失效
				return mv;
			}
			
			/*   调用身份证接口   */
			@RequestMapping("sfz.do")
			public Map<String, String>  sfz(String realname,String idcard) {
				ModelAndView mv = new ModelAndView();
				Map<String,String> map = InterfaceModule.idCardInterface(realname, idcard);
				String mapSfz = map.get("compareStatusDesc");
				if("一致".equals(mapSfz)) {
					map.put("mesg", "true");
				}else {
					map.put("mesg", "false");
				}//你来
				return map;
			}




}
